﻿using DapperExtensions;
using SAICAPPWEB.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;

namespace SAICAPPWEB.BLL
{
    public class UserInfoBll:BaseBll<UserInfo>
    {
        const string Current_User_Session_Key = "CurrentUser";
        const string Current_User_IsDefault = "Y";
        private UserInfo _currentUser;
        /// <summary>
        /// 默认测试用户
        /// </summary>
        public static string TestUser
        {
            get
            {
                return ConfigurationManager.AppSettings["TestUser"];
            }
        }
        /// <summary>
        /// 当前用户
        /// </summary>
        public UserInfo CurrentUser
        {
            get
            {
                //如果不在Web环境则使用测试用户帐号
                if (HttpContext.Current == null)
                    _currentUser = this.GetModel(TestUser);

                if (_currentUser == null)
                {
                    if (null != HttpContext.Current.Session)
                        _currentUser = HttpContext.Current.Session[Current_User_Session_Key] as UserInfo;
                    else
                    {
                        _currentUser = this.GetModel(this.GetCurrentUserIdentity());
                        return _currentUser;
                    }
                    if (_currentUser == null)
                    {

                        _currentUser = this.GetModel(this.GetCurrentUserIdentity());

                        if (_currentUser == null)
                            throw new Exception(String.Format("User [{0}] Not Exist In System", this.GetCurrentUserIdentity()));
                        //this.Update(_currentUser);//更新最后登录时间
                        HttpContext.Current.Session[Current_User_Session_Key] = _currentUser;

                    }
                }
              
                return _currentUser;
            }
            private set
            {
                _currentUser = value;
                HttpContext.Current.Session[Current_User_Session_Key] = value;
            }
        }
        private string GetCurrentUserIdentity()
        {
                return HttpContext.Current.User.Identity.Name.Trim().ToLower();
        }
        /// <summary>
        /// 模拟用户
        /// </summary>
        /// <param name="account"></param>
        public void ImpersonateUser(string account)
        {
            UserInfo user = GetModel(account);

            if (user == null)
                throw new Exception(String.Format("用户{0}不存在/User {0} not exist", account));
            CurrentUser = user;
        }
        public UserInfo GetModel(string account)
        {
            var predicate = Predicates.Field<UserInfo>(p => p.UserAccount, Operator.Eq, account);
            if (Db.Connection.State != ConnectionState.Open)
                Db.Connection.Open();
            var user = Db.GetList<UserInfo>(predicate).FirstOrDefault();
            Db.Connection.Close();
            return user;
        }
    }
}